{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "lesbian-springer",
   "metadata": {},
   "source": [
    "## Inference Examples\n",
    "\n",
    "We will show:\n",
    "\n",
    " * Model inference to a Tensorflow model\n",
    "   * REST and gRPC using seldon CLI, curl and grpcurl\n",
    " * Pipeline inference\n",
    "   * REST and gRPC using seldon CLI, curl and grpcurl\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2cb9237c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "env: INFER_ENDPOINT=0.0.0.0:9000\n"
     ]
    }
   ],
   "source": [
    "%env INFER_ENDPOINT=0.0.0.0:9000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30431134",
   "metadata": {},
   "source": [
    "### Tensorflow Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "34165288",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: tfsimple1\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\r\n",
      "  requirements:\r\n",
      "  - tensorflow\r\n",
      "  memory: 100Ki\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/tfsimple1.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "275a8445",
   "metadata": {},
   "source": [
    "Load the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "726f2dd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/tfsimple1.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b141e73",
   "metadata": {},
   "source": [
    "Wait for the model to be ready."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2d9b631f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status tfsimple1 -w ModelAvailable | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7abd240a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"tfsimple1_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"OUTPUT0\",\r\n",
      "\t\t\t\"datatype\": \"INT32\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t16\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2,\r\n",
      "\t\t\t\t4,\r\n",
      "\t\t\t\t6,\r\n",
      "\t\t\t\t8,\r\n",
      "\t\t\t\t10,\r\n",
      "\t\t\t\t12,\r\n",
      "\t\t\t\t14,\r\n",
      "\t\t\t\t16,\r\n",
      "\t\t\t\t18,\r\n",
      "\t\t\t\t20,\r\n",
      "\t\t\t\t22,\r\n",
      "\t\t\t\t24,\r\n",
      "\t\t\t\t26,\r\n",
      "\t\t\t\t28,\r\n",
      "\t\t\t\t30,\r\n",
      "\t\t\t\t32\r\n",
      "\t\t\t]\r\n",
      "\t\t},\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"OUTPUT1\",\r\n",
      "\t\t\t\"datatype\": \"INT32\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t16\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer tfsimple1 --inference-host ${INFER_ENDPOINT} \\\n",
    "    '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f86c494a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"modelName\":\"tfsimple1_1\",\"modelVersion\":\"1\",\"outputs\":[{\"name\":\"OUTPUT0\",\"datatype\":\"INT32\",\"shape\":[\"1\",\"16\"],\"contents\":{\"intContents\":[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32]}},{\"name\":\"OUTPUT1\",\"datatype\":\"INT32\",\"shape\":[\"1\",\"16\"],\"contents\":{\"intContents\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}}]}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer tfsimple1 --inference-mode grpc  --inference-host ${INFER_ENDPOINT} \\\n",
    "    '{\"model_name\":\"tfsimple1\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"intContents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"intContents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f4f1f4b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"model_name\":\"tfsimple1_1\",\"model_version\":\"1\",\"outputs\":[{\"name\":\"OUTPUT0\",\"datatype\":\"INT32\",\"shape\":[1,16],\"data\":[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32]},{\"name\":\"OUTPUT1\",\"datatype\":\"INT32\",\"shape\":[1,16],\"data\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}]}"
     ]
    }
   ],
   "source": [
    "!curl http://${INFER_ENDPOINT}/v2/models/tfsimple1/infer -H \"Content-Type: application/json\" -H \"seldon-model: tfsimple1\" \\\n",
    "        -d '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a9471bff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"modelName\": \"tfsimple1_1\",\r\n",
      "  \"modelVersion\": \"1\",\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT0\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ]\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT1\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ]\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"rawOutputContents\": [\r\n",
      "    \"AgAAAAQAAAAGAAAACAAAAAoAAAAMAAAADgAAABAAAAASAAAAFAAAABYAAAAYAAAAGgAAABwAAAAeAAAAIAAAAA==\",\r\n",
      "    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\"\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!grpcurl -d '{\"model_name\":\"tfsimple1\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' \\\n",
    "    -plaintext \\\n",
    "    -import-path ../apis \\\n",
    "    -proto ../apis/mlops/v2_dataplane/v2_dataplane.proto \\\n",
    "    -rpc-header seldon-model:tfsimple1 \\\n",
    "    ${INFER_ENDPOINT} inference.GRPCInferenceService/ModelInfer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3720658f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: tfsimple\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: tfsimple1\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - tfsimple1\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/tfsimple.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "050ed2dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/tfsimple.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5fc982b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipelineName\":\"tfsimple\",\"versions\":[{\"pipeline\":{\"name\":\"tfsimple\",\"uid\":\"cg5fm6c6dpcs73c4qhe0\",\"version\":1,\"steps\":[{\"name\":\"tfsimple1\"}],\"output\":{\"steps\":[\"tfsimple1.outputs\"]},\"kubernetesMeta\":{}},\"state\":{\"pipelineVersion\":1,\"status\":\"PipelineReady\",\"reason\":\"created pipeline\",\"lastChangeTimestamp\":\"2023-03-10T09:40:41.317797761Z\",\"modelsReady\":true}}]}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline status tfsimple -w PipelineReady"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "42efbedd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"\",\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2,\r\n",
      "\t\t\t\t4,\r\n",
      "\t\t\t\t6,\r\n",
      "\t\t\t\t8,\r\n",
      "\t\t\t\t10,\r\n",
      "\t\t\t\t12,\r\n",
      "\t\t\t\t14,\r\n",
      "\t\t\t\t16,\r\n",
      "\t\t\t\t18,\r\n",
      "\t\t\t\t20,\r\n",
      "\t\t\t\t22,\r\n",
      "\t\t\t\t24,\r\n",
      "\t\t\t\t26,\r\n",
      "\t\t\t\t28,\r\n",
      "\t\t\t\t30,\r\n",
      "\t\t\t\t32\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"name\": \"OUTPUT0\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t16\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT32\"\r\n",
      "\t\t},\r\n",
      "\t\t{\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0,\r\n",
      "\t\t\t\t0\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"name\": \"OUTPUT1\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t16\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT32\"\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer tfsimple  --inference-host ${INFER_ENDPOINT} \\\n",
    "    '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "db8adc42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"outputs\":[{\"name\":\"OUTPUT0\",\"datatype\":\"INT32\",\"shape\":[\"1\",\"16\"],\"contents\":{\"intContents\":[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32]}},{\"name\":\"OUTPUT1\",\"datatype\":\"INT32\",\"shape\":[\"1\",\"16\"],\"contents\":{\"intContents\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}}]}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer tfsimple --inference-mode grpc  --inference-host ${INFER_ENDPOINT} \\\n",
    "    '{\"model_name\":\"tfsimple1\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "23957098",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"model_name\":\"\",\"outputs\":[{\"data\":[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32],\"name\":\"OUTPUT0\",\"shape\":[1,16],\"datatype\":\"INT32\"},{\"data\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"name\":\"OUTPUT1\",\"shape\":[1,16],\"datatype\":\"INT32\"}]}"
     ]
    }
   ],
   "source": [
    "!curl http://${INFER_ENDPOINT}/v2/models/tfsimple1/infer -H \"Content-Type: application/json\" -H \"seldon-model: tfsimple.pipeline\" \\\n",
    "        -d '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "21d5cfb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT0\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ]\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT1\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ]\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"rawOutputContents\": [\r\n",
      "    \"AgAAAAQAAAAGAAAACAAAAAoAAAAMAAAADgAAABAAAAASAAAAFAAAABYAAAAYAAAAGgAAABwAAAAeAAAAIAAAAA==\",\r\n",
      "    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\"\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!grpcurl -d '{\"model_name\":\"tfsimple1\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' \\\n",
    "    -plaintext \\\n",
    "    -import-path ../apis \\\n",
    "    -proto ../apis/mlops/v2_dataplane/v2_dataplane.proto \\\n",
    "    -rpc-header seldon-model:tfsimple.pipeline \\\n",
    "    ${INFER_ENDPOINT} inference.GRPCInferenceService/ModelInfer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8ee2b2fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline unload tfsimple\n",
    "!seldon model unload tfsimple1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9331e253",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
